/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 1.14C */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Toolkit headers */
#include <Ph.h>
#include <Pt.h>
#include <Ap.h>

/* Local headers */
#include "ph_toolBar.h"
#include "abimport.h"
#include "proto.h"

extern WIN_DATCURS localCursTyp;
extern RL localRl;

int manageProcess( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	PtArg_t args[2];
	PhWindowEvent_t *winEvent;
	PhWindowEvent_t newEvent;
	long winState;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	/* get the current state of the window */
	winState = PtWindowGetState(widget);

	/* get pointer to the window event structure */
	winEvent = (PhWindowEvent_t *)cbinfo->cbdata;

	/* check to see which type event evoked this callback */
	if(winEvent->event_f == Ph_WM_CLOSE) {
		/* if the user tries to close the window (kill the process),
		 * make sure that scribe is not keeping data
		 */
		if(i_b->i_flags & (I_EOUT | I_AOUT)) {
			PtAskQuestion(ABW_base, NULL,
						  "Stop keeping data\nbefore killing process!", "helv12b",
						  "OK", NULL, NULL, 1);
			return( Pt_CONTINUE );
		}
		else {
			if(i_b->i_flags & I_GO) {
				ad_stop();
				clk_stop();
			}
			release_(&myptp->p_sem);
			myptp->p_state |= P_EXIT_ST;
			myptp->p_id = 0;
#ifdef CLRMIRRORS
			XYmout(0, 0);	/* put mirrors at 0, 0 */
#endif
#ifdef PCS_SOCKET
			pcsCloseVex();		/* close tcp/ip socket to vex */
#endif

/* Long added,3-23-07 per jig's CHANGES file  jig added re: Jochy */
#ifdef SIG_SOCKET
   sigSocket_close(); /* close socket */
#endif
			exit(EXIT_SUCCESS);
		}
	}
	else if((winEvent->event_f == Ph_WM_ICON) && (winState & Ph_WM_STATE_ISICONIFIED)) {
		if(i_b->i_flags & (I_EOUT | I_AOUT)) {
			PtAskQuestion(ABW_base, NULL,
						  "Stop keeping data\nbefore stopping process!", "helv12b",
						  "OK", NULL, NULL, 1);

			memset(&newEvent, 0, sizeof(newEvent));
			newEvent.event_f = Ph_WM_RESTORE;
			newEvent.event_state = Ph_WM_EVSTATE_DEICONIFY;
			newEvent.rid = PtWidgetRid(ABW_base);
			PtForwardWindowEvent(&newEvent);
			return( Pt_CONTINUE );
		}
		else {
			if(i_b->i_flags & I_GO) {
				/* stop the clock if it is running */
				PtSetArg(&args[0], Pt_ARG_ONOFF_STATE, 0, 0);
				PtSetArg(&args[1], Pt_ARG_TEXT_STRING, "Clock\nOff", 0);
				PtSetResources(ABW_clockOnOffButton, 2, args);
				ad_stop();
				clk_stop();
			}

			/* copy the real time display variables from shared memory to local memory */
			localCursTyp = i_b->cursTyp;
			localRl = i_b->rl;
			
			/* signal procSwitch that this process is stopped */
			release_(&myptp->p_sem);
			myptp->p_state &= ~P_RUN_ST;

			if(i_b->ph_int_pi == myptx) i_b->ph_int_pi = -1;

#ifdef PCS_SOCKET
			pcsCloseVex();
#endif

		}
	}
	else if((winEvent->event_f == Ph_WM_ICON) && !(winState & Ph_WM_STATE_ISICONIFIED)) {
		if(i_b->ph_int_pi > 0) {
			memset(&newEvent, 0, sizeof(newEvent));
			newEvent.event_f = Ph_WM_ICON;
			newEvent.event_state = Ph_WM_EVSTATE_ICONIFY;
			PtForwardWindowTaskEvent(i_b->ptbl[i_b->ph_int_pi].p_coid, &newEvent);
		}
		
		/* copy real time display variables from local storage to shared memory */
		i_b->cursTyp = localCursTyp;
		i_b->rl = localRl;

		myptp->p_state |= P_RUN_ST;
		i_b->ph_int_pi = myptx;
	}

	return( Pt_CONTINUE );
}

